#define _SOFT_SPI_C_
/* Includes ---------------------------------------------------------*/
/* USER CODE BEGIN Includes */
//#include "fm33lg0xx.h"
#include "soft_spi.h"
/* USER CODE END Includes */

/* Types ------------------------------------------------------------*/
/* USER CODE BEGIN Types */

/* USER CODE END Types */

/* Constants --------------------------------------------------------*/
/* USER CODE BEGIN Constants */

/* USER CODE END Constants */

/* Macro ------------------------------------------------------------*/
/* USER CODE BEGIN Macro */
#define SPI_SCK_H()                                         (FL_GPIO_SetOutputPin(GPIOC, FL_GPIO_PIN_8))
#define SPI_SCK_L()                                         (FL_GPIO_ResetOutputPin(GPIOC, FL_GPIO_PIN_8))

#define SPI_MOSI_H()                                        (FL_GPIO_SetOutputPin(GPIOC, FL_GPIO_PIN_10))
#define SPI_MOSI_L()                                        (FL_GPIO_ResetOutputPin(GPIOC, FL_GPIO_PIN_10))

#define SPI_CS_H()                                          (FL_GPIO_SetOutputPin(GPIOC, FL_GPIO_PIN_7))
#define SPI_CS_L()                                          (FL_GPIO_ResetOutputPin(GPIOC, FL_GPIO_PIN_7))
/* USER CODE END Macro */

/* Variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */

/* Function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FP */
void vDelay(uint32_t ulTime);
/* USER CODE END FP */

void vSoftSPIInit(void)
{
	// MOSI PC10:Output
	// MISO PC9:Input
	// SCK: PC8:Output
	// CS:  PC7:Output
	FL_GPIO_InitTypeDef GPIO_InitStruct;
	
	// MOSI,SCK,CS
    GPIO_InitStruct.pin = FL_GPIO_PIN_10 | FL_GPIO_PIN_8 | FL_GPIO_PIN_7;
    GPIO_InitStruct.mode = FL_GPIO_MODE_OUTPUT;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_DISABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;
    GPIO_InitStruct.analogSwitch = FL_DISABLE;
    FL_GPIO_Init(GPIOC, &GPIO_InitStruct);                                                                                                                                                                                                                                                                                       
	
	// MISO
    GPIO_InitStruct.pin = FL_GPIO_PIN_9;
    GPIO_InitStruct.mode = FL_GPIO_MODE_INPUT;
    FL_GPIO_Init(GPIOC, &GPIO_InitStruct);
	
	SPI_CS_L();
}

void vSPIWrite(uint8_t *pucOutData, uint8_t *pucInData, uint8_t ucLen)
{
	uint8_t i = 0;
	
	for(i = 0; i < ucLen; i++)
	{
		pucInData[i] = ucSPIWriteByte(pucOutData[i]);
	}
}

uint8_t ucSPIWriteByte(uint8_t ucData)
{
    // CPOL = 0, CPHA = 1, MSB first
	uint8_t i = 0;
	uint8_t ucReadData = 0;
	
	for(i = 0; i < 8; i++)
	{
		// MO
		SPI_SCK_H();
		if(ucData & 0x80)
		{
			SPI_MOSI_H();
		}
		else
		{
			SPI_MOSI_L();
		}
		ucData <<= 1;
		vDelay(2);

		// MI
		SPI_SCK_L();
		ucReadData <<= 1;
		if(SPI_MISO())
		{
			ucReadData++;
		}
		vDelay(2);
	}
	
	return ucReadData;
}

void vDelay(uint32_t ulTime)
{
	uint32_t ulCnt = ulTime;
	
	while(ulCnt--);
}
